Ext.define('BenBen.modules.project.operation.OperationWorkModule',{
    extend : 'Ext.ux.desktop.Module',
    requires: [
        'Ext.ux.form.MultiComboBox',
        'BenBen.common.BasePanel',
        'BenBen.modules.project.operation.OperationWorkEditForm',
        'BenBen.modules.project.operation.OperationWorkDistributeWindow',
        'BenBen.modules.project.operation.OperationWorkHandleWindow'
    ],
    id : 'operationWork-win',
    border : false,
    init : function(){
        this.launcher = {
            text: '工作清单',
            iconCls:'icon-grid'
        };
    },
    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('operationWork-win');

        if (!win) {
            var dataModelFields = ['id','workNo','startTime','endTime', 'departmentName', 'grade','hitchBigType','trueName','phone','email','trueName','state','handlers','describe','picUrl','feedbackGrade'];
            
            var buttons = getButtons("operationWork-win");
            
            var columns = [{
                dataIndex: 'workNo',
                text: '工作编号'
            },{
            	dataIndex: 'startTime',
            	text: '日期'
            },{
                dataIndex: 'departmentName',
                text: '部门'
            },{
                dataIndex: 'trueName',
                text: '用户'
            },{
                dataIndex: 'phone',
                text: '联系电话'
            },{
            	dataIndex: 'email',
            	text: 'Email'
            },{
            	dataIndex: 'hitchBigType',
            	text: '故障类型'
            },{
                dataIndex: 'grade',
                text: '等级',
                renderer: function (value) {
                    if (value == '1') {
                        return "重要";
                    }else if (value == '2') {
                        return "普通";
                    } 
                }
            },{
            	dataIndex: 'handlers',
            	text: '处理人'
            },{
                dataIndex: 'endTime',
                text: '完成时间'
            },{
                dataIndex: 'feedbackGrade',
                text: '评价级别',
            	renderer: function (value) {
            		if (value == 1) {
            			return "不满意";
            		}else if (value == 2) {
            			return "一般";
            		}else if (value == 3) {
            			return "满意";
            		}else if (value == 4) {
            			return "非常满意";
            		}else {
            			return "未评价";
            		} 
            	}
            },{
            	dataIndex: 'state',
            	text: '状态',
            	renderer: function (value) {
            		if (value == 1) {
            			return "未处理";
            		}else if (value == 2) {
            			return "处理中";
            		}else if (value == 3) {
            			return "挂起";
            		}else if (value == 4) {
            			return "已完成";
            		}else if (value == 5) {
            			return "已反馈";
            		}else if (value == 0) {
            			return "取消";
            		} 
            	}
            }];

            var searchItem = [{
                xtype: 'textfield',
                name: 'username',
                isSearch: true,
                fieldLabel: '用户',
                labelAlign: 'right'
            },{
                xtype: 'textfield',
                name: 'handlers',
                isSearch: true,
                fieldLabel: '处理人',
                labelAlign: 'right'
            },{
            	xtype: 'multicombobox',
            	name: 'states',
            	isSearch: true,
            	fieldLabel: '状态',
            	labelAlign: 'right',
            	mode : 'local',
                editable : false,
                triggerAction : 'all',
                displayField : 'name',
                valueField : 'value',
                store : new Ext.data.SimpleStore({
                    fields : ['value','name'],
                    data : [[1,'未处理'],[2,'处理中'],[3,'挂起'],[4,'已完成'],[5,'已反馈']]
                })
            },{
            	xtype: 'multicombobox',
            	isSearch: true,
            	mode : 'local',
            	name: 'workOrigins',
                fieldLabel: '工作来源',
                mode : 'local',
                editable : false,
                labelAlign : 'right',
                value : '',
                triggerAction : 'all',
                displayField : 'name',
                valueField : 'name',
                store : new Ext.data.SimpleStore({
                    fields : ['name'],
                    data : [['电话'],['邮件'],['短信'],['微信'],['口头'],['OA']]
                })
            },{
                xtype: 'datefield',
                isSearch: true,
                fieldLabel: '日期(始)',
            	labelAlign : 'right',
            	format: 'Ymd', 
                selectOnFocus:true, 
                editable:false, 
            	name  :'startDate'
            },{
            	xtype: 'datefield',
            	isSearch: true,
            	fieldLabel: '日期(止)',
            	labelAlign : 'right',
            	format: 'Ymd', 
            	selectOnFocus:true, 
            	editable:false, 
            	name  :'endDate'
            }];

            var dataForm = Ext.create('BenBen.modules.project.operation.OperationWorkEditForm', {
            	moreComponent : true
            });

            var tbarItems = [];
            
            if(buttons.indexOf('distribute') != -1){
            	tbarItems.push({
	                text : '分配',
	                iconCls : 'gridupdate',
	                handler : function(){
	                	var record = basePanel.getSeletedRecord();
	                    if(record != null && record.get('state') == 1){
	                    	var distributeWin =  Ext.create('BenBen.modules.project.operation.OperationWorkDistributeWindow',{
	                    		workId : record.get('id'),
	                    		store : basePanel.getStore()
	                        });
	                        
	                    	distributeWin.show();
	                    }
	                	
	                }
	            });
            }
            if(buttons.indexOf('handle') != -1){
            	tbarItems.push({
                    text : '处理',
                    iconCls : 'gridupdate',
                    handler : function(){
                    	var record = basePanel.getSeletedRecord();
                        if(record != null && (record.get('state') == 2 || record.get('state') == 3)){
                        	var shiftWin =  Ext.create('BenBen.modules.project.operation.OperationWorkHandleWindow',{
                        		record : record,
                        		store : basePanel.getStore()
                            });
                        	shiftWin.show();
                        }
                    }
                });
            }
            if(buttons.indexOf('export') != -1){
            	tbarItems.push({
            		text : '导出',
            		iconCls : 'gridupdate',
            		handler : function(){
            			var config = {
        					url : 'project/operationWork/export',
        					cm : [{
        		                dataIndex: 'workNo',
        		                text: '工作编号'
        		            },{
        		            	dataIndex: 'startTime',
        		            	text: '日期'
        		            },{
        		                dataIndex: 'departmentName',
        		                text: '部门'
        		            },{
        		                dataIndex: 'trueName',
        		                text: '用户'
        		            },{
        		                dataIndex: 'phone',
        		                text: '联系电话'
        		            },{
        		            	dataIndex: 'email',
        		            	text: 'Email'
        		            },{
        		            	dataIndex: 'hitchBigType',
        		            	text: '故障类型'
        		            },{
        		            	dataIndex: 'describe',
        		            	text: '描述'
        		            },{
        		            	dataIndex: 'handlers',
        		            	text: '处理人'
        		            },{
        		                dataIndex: 'endTime',
        		                text: '完成时间'
        		            },{
        		            	dataIndex: 'feedbackGrade',
        		            	text: '评价级别'
        		            },{
        		            	dataIndex: 'state',
        		            	text: '状态(1未处理,2处理中,3挂起,4已完成,5已反馈)'
        		            }]
        				};
            			basePanel.exportWithGridsMsg(config);
            		}
            	});
            }
            
            var basePanel = Ext.create('BenBen.common.BasePanel', {
                columns : columns,
                searchItem : searchItem,
                dataForm : dataForm,
                dataModelFields : dataModelFields,
                gridPlugins : [{
                    ptype: 'rowexpander',
                    rowBodyTpl : [
                        '<p style="margin-left:60px;"><b>工作描述:</b> {describe}</p>',
                        '<p style="margin-left:60px;"><b>报修图片:</b><img src="{picUrl}" style="width:200px;height:200px;" /> </p>'
                    ]
                }],
                tbarDefaultItems : buttons,
                tbarItems : tbarItems,
                config : {
                    url : 'project/operationWork',
                    pageSize : 30,
                    winWidth : 800,
                    winHeigth : 480
                }
            });
            var countPanel = Ext.create('Ext.panel.Panel', {
        	    width: 800,
        	    frame : true,
        	    border : false,
        	    html: '没有统计数据'
        	});
            win = desktop.createWindow({
                id: 'operationWork-win',
                title: '工作清单',
                width: 800,
                height: 700,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'form',
                items: [{
    				xtype : 'panel',
    				layout : 'fit',
    				items : [basePanel]
    			},{
    				xtype : 'panel',
    				layout : 'fit',
    				items : [countPanel]
    			}]
            });
            Ext.Ajax.request({
                url: 'project/operationWork/count',
                method: 'GET',
                callback: function (options, success, response) {
                    if (success) {
                    	var data = Ext.decode(response.responseText).data[0];
                        
                        var html = '<p>未处理：'+data.notHandle
                        	+'&nbsp; 处理中：'+data.handling
                        	+'&nbsp; 挂起：'+data.handup
                        	+'&nbsp; 已处理：'+data.handled
                        	+'</p>';
                        countPanel.body.update(html);
                    }
                }
            });
        }


        return win;
	}
});
